<template>
  <div class="app-container">
    <div class="filter-container">
      标题:
      <el-input v-model="listQuery.name" label="标题" placeholder="关键词" style="width: 200px;" class="filter-item mring10" @keyup.enter.native="handleFilter" suffix-icon="el-icon-search"/>
      <el-button v-waves class="filter-item mring10" type="primary" icon="el-icon-search" @click="handleFilter" size="small"> 搜索</el-button>
      <el-button v-waves  type="success" icon="el-icon-plus" @click="handleCreate()" size="small"> 添加菜单</el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      fit
      stripe
      highlight-current-row
      style="width: 100%;margin-top: 1.125rem"
      row-key="id"
    >
      <el-table-column label="菜单名" prop="name" min-width="150"></el-table-column>
      <el-table-column label="图标" prop="icon" min-width="120">
        <template slot-scope="{row}">
          <img  v-if="row.icon" :src="row.icon" class="avatar"  style="max-width: 80px;height: auto;  float:left;margin-right:5px;border:1px solid rgb(142 203 255);padding: 2px;">
        </template>
      </el-table-column>
      <el-table-column label="链接" prop="link" min-width="280"></el-table-column>
      <el-table-column label="排序" prop="sort" min-width="100"></el-table-column>
      
      <el-table-column label="创建时间" prop="create_time" min-width="150" align="center">
        <template slot-scope="{row}">
           {{ row.create_time | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
           <el-button type="primary" @click="handleUpdate(row)" size="small">编 辑</el-button>
           <el-button type="danger" @click="handleDelete(row,$index)" size="small">删 除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination hide-on-single-page v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="getList" />
  </div>
</template>
<script>
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination'
import { fetchList ,deleteIndexMenu} from '@/api/config/indexMenu'

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      list: [],
      tableKey:'',
      listLoading: false,
      total:0,
      listQuery: {
        size:10,
        page:1
      }
  }},
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.$router.push({name:'createIndexMenu'})
    },
    handleUpdate(row) {
      this.$router.push({name:'createIndexMenu',params:{id:row.id}})
    },
    handleDelete(row, index) {
      deleteIndexMenu(row).then(response => {
        if(response.code == 200){
          this.$notify({
            title: 'Success',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })

          this.list.splice(index, 1)
        }
      })
    }
  }
}

</script>
<style type="text/css">
  .filter-container{background: #FFFFFF;padding: 0.625rem 1.125rem;}
  table td{font-size: 12px;color: #8c8c8c;}
  .filter-item input[type='text'] {
  height: 32px;
  line-height: 32px;
  font-size: 12px;
}

.mring10 {
  margin-right: 10px;
}
</style>
